package com.shuqi.activity.viewport;

import com.shuqi.common.utils.Util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class MyExpandableListView extends ExpandableListView implements OnScrollListener {

    @Override
    public void setAdapter(ExpandableListAdapter adapter) {
        super.setAdapter(adapter);
    }

    private ExpandableListAdapter mExAdapter;
    private RelativeLayout mGroupLayout;
    public int mGroupIndex = -1;
    

    /**
     * @param context
     */
    public MyExpandableListView(Context context) {
        super(context);
        super.setOnScrollListener(this);
    }

    /**
     * @param context
     * @param attrs
     */
    public MyExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOnScrollListener(this);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MyExpandableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        super.setOnScrollListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {

        if (mExAdapter == null)
            mExAdapter = this.getExpandableListAdapter();

        int ptp = view.pointToPosition(0, 0);
        if (ptp != AdapterView.INVALID_POSITION) {
            MyExpandableListView qExlist = (MyExpandableListView) view;
            long pos = qExlist.getExpandableListPosition(ptp);
            int groupPos = ExpandableListView.getPackedPositionGroup(pos);
            int childPos = ExpandableListView.getPackedPositionChild(pos);


            if (childPos < 0) {
                groupPos = -1;
            }
            if (groupPos < mGroupIndex) {
                mGroupIndex = groupPos;

                if (mGroupLayout != null) {
                    mGroupLayout.removeAllViews();
                    mGroupLayout.setVisibility(GONE);// 这里设置Gone 为了不让它遮挡后面header
                }
            } else if (groupPos > mGroupIndex) {
                final FrameLayout fl = (FrameLayout) getParent();
                mGroupIndex = groupPos;
                if (mGroupLayout != null)
                    fl.removeView(mGroupLayout);

                mGroupLayout =
                        (RelativeLayout) getExpandableListAdapter().getGroupView(groupPos, true,
                                null, null);
                mGroupLayout.setBackgroundDrawable(view.getBackground());
                mGroupLayout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        collapseGroup(mGroupIndex);
                    }
                });


                fl.addView(mGroupLayout, fl.getChildCount(), new LayoutParams(
                        LayoutParams.FILL_PARENT, Util.dip2px(getContext(), 55)));

            }
        }
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {}

}
